package com.example.wyemail.util;

import org.sikuli.script.*;

import java.io.File;
import java.io.IOException;

/**
 * 邮箱帐户工具类
 *
 * @author fxab
 * @date 2024/06/07
 */
public class EmailAccountUtil {
    /**
     * 添加邮箱帐户
     *
     * @param email    电子邮件
     * @param password 密码
     * @throws InterruptedException   中断异常
     * @throws IOException            ioexception
     * @throws IconUtil.ImageNotFoundException 未找到图像异常
     * @throws FindFailed             查找失败
     */
    public static void addEmailAccount(String email, String password) throws InterruptedException, IOException, IconUtil.ImageNotFoundException, FindFailed {
        // 添加邮箱账号的逻辑
        File directory = new File("");
        String courseFile = directory.getCanonicalPath() ;
        String imgPath = courseFile + "\\src\\main\\resources\\images\\";
        Screen s = new Screen();
        String addEmailImg = imgPath + "addEmail.png";
        String emailAccountImg = imgPath + "emailAccount.png";
        String passwordImg = imgPath + "password.png";
        String loginImg = imgPath + "login.png";
        String noPhoneImg = imgPath + "noPhone.png";
        String emailExistImg = imgPath + "emailExist.png";
        if(IconUtil.IsExist(s,addEmailImg)){
            s.click(addEmailImg);
        }
        if(IconUtil.IsExist(s,emailAccountImg)){
            s.click(emailAccountImg);
            s.paste(email);
        }
        if (s.exists(emailExistImg, 1) != null) {
            s.click(emailExistImg);
        }else {
            if(IconUtil.IsExist(s,passwordImg)){
                s.click(passwordImg);
                s.paste(password);
            }
            Thread.sleep(300);
            if(IconUtil.IsExist(s,loginImg)){
                s.click(loginImg);
            }
        }
        //密码错误
        if (s.exists(loginImg, 1.2) != null) {  // 等待 1 秒查找图像
            Pattern login = new Pattern(loginImg);
            Match match = s.find(login);
            Location topLeft = match.getTopLeft();
            int topLeftX = topLeft.getX();
            int topLeftY = topLeft.getY();
            int targetX = topLeftX + 180;
            int targetY = topLeftY - 310;
            s.click(new Location(targetX, targetY));
            return;
        }


        //关闭绑定手机号弹窗
        if (s.exists(noPhoneImg, 1.2) != null) {  // 等待 1 秒查找图像
            Pattern noPhone = new Pattern(noPhoneImg);
            Match match = s.find(noPhone);
            Location topLeft = match.getTopLeft();
            int topLeftX = topLeft.getX();
            int topLeftY = topLeft.getY();
            int targetX = topLeftX + 200;
            int targetY = topLeftY - 45;
            s.click(new Location(targetX, targetY));
        }


    }
}
